package com.bear.sgg.springboot_11_cache.config;

import com.bear.sgg.springboot_11_cache.entity.Employee;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;


/**
 * @ClassNmae CacheConfig
 * @Description TODO
 * @Author bear
 * @Date 2020/3/17 0017 上午 11:34
 */
@Configuration
public class CacheConfig {

    /**
     * 自定义键值生成器
     * @return
     */
    @Bean("myKeyGenerator")
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                return "[" + params[0] + "]";
            }
        };
    }

    @Bean("myKeyGenerator2")
    public KeyGenerator keyGenerator2() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                Employee employee = (Employee) params[0];
                return "[" + employee.getId() + "]";
            }
        };
    }
}
